//
//  ViewController.m
//  回调Block
//
//  Created by clz on 2019/7/30.
//  Copyright © 2019年 clz. All rights reserved.
//

#import "ViewController.h"
#import "TestView.h"
#import "FirstViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //block
    /*
     代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,Block是一种特殊的数据类型,其可以正常定义变量、作为参数、作为返回值；Block被广泛运用在回调。
     
     注: Block的声明与赋值只是保存了一段代码段,必须调用才能执行内部代码.
     */
    
    TestView *view = [[TestView alloc]init];
    view.frame = CGRectMake(self.view.frame.size.width/2.0 - 150, 300, 300, 50);
    [self.view addSubview: view];
//    view.changeColor = ^{
//        //内部实现的代码
//        self.view.backgroundColor = [UIColor greenColor];
//    };
    
    view.changeColor = ^(NSString * _Nonnull test) {
        NSLog(@"test = %@",test);
    };
    
//    [view userBlock:^(NSString * _Nonnull str) {
//        NSLog(@"str = %@",str);
//        self.view.backgroundColor = [UIColor greenColor];
//    }];
    
    [view userBlock:^(NSString * _Nonnull str) {
        NSLog(@"str = %@",str);
        self.view.backgroundColor = [UIColor greenColor];

    }];
    
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor=[UIColor redColor];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"按钮1" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:18];
    button.frame = CGRectMake(0, 100, 300, 50);
    
    [self.view addSubview:button];
    
}

- (void)buttonAction:(UIButton *)sender{
    NSLog(@"按钮1事件");
//    self.view.backgroundColor = [UIColor yellowColor];
    FirstViewController *firstViewControlelr = [[FirstViewController alloc]init];
    [self presentViewController:firstViewControlelr animated:YES completion:^{
        
    }];
    
    
}


@end
